<?php
if (!file_exists('../install.lock')) {
    header('Location: ../install.php');
    exit;
}

require_once 'config.php';

$settings = getSettings();
$order = null;

$order_no = $_GET['order_no'] ?? '';

if (empty($order_no)) {
    header('Location: index.php');
    exit;
}

// 获取订单信息
$stmt = $pdo->prepare("SELECT o.*, p.name as product_name, p.description as product_description FROM orders o LEFT JOIN products p ON o.product_id = p.id WHERE o.order_no = ?");
$stmt->execute([$order_no]);
$order = $stmt->fetch();

if (!$order || $order['status'] !== 'paid') {
    header('Location: index.php');
    exit;
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui">
    <title><?php echo htmlspecialchars($settings['title'] ?? '卡密购买'); ?> - 支付成功</title>
    <link rel="shortcut icon" type="image/x-icon" href="../assets/favicon.ico">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-touch-fullscreen" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <link rel="stylesheet" type="text/css" href="../assets/css/materialdesignicons.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/animate.min.css">
    <link rel="stylesheet" type="text/css" href="../assets/css/style.min.css">
    <style>
        .success-container {
            max-width: 600px;
            margin: 0 auto;
        }
        .success-icon {
            font-size: 80px;
            color: #28a745;
        }
        .kami-card {
            background: #f8f9fa;
            border: 2px dashed #dee2e6;
            border-radius: 10px;
            padding: 20px;
            margin: 20px 0;
        }
        .kami-item {
            background: white;
            border: 1px solid #dee2e6;
            border-radius: 5px;
            padding: 14px 18px;
            margin: 8px 0;
            font-family: monospace;
            font-size: 18px;
            word-break: break-all;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .copy-btn {
            cursor: pointer;
            color: #007bff;
            font-size: 20px;
            padding: 4px 10px;
        }
        .copy-btn:hover {
            color: #0056b3;
            background: #f0f0f0;
            border-radius: 4px;
        }
        .card.mb-4, .card {
            margin-bottom: 2rem !important;
        }
        .text-center.mt-4 {
            margin-top: 2.5rem !important;
        }
        main.lyear-layout-content {
            padding-top: 0 !important;
            margin-top: 0 !important;
            background: none !important;
            box-shadow: none !important;
        }
    </style>
</head>

<body>
<!--页面loading-->
<div id="lyear-preloader" class="loading">
    <div class="ctn-preloader">
        <div class="round_spinner">
            <div class="spinner"></div>
            <img src="../assets/images/loading-logo.png" alt="">
        </div>
    </div>
</div>
<!--页面loading end-->

<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!-- 导航栏 -->
        <nav class="navbar navbar-expand-lg navbar-light bg-white border-bottom w-100" style="margin:0;padding:0;">
            <div class="container-fluid px-4">
                <a class="navbar-brand" href="index.php">
                    <?php echo htmlspecialchars($settings['title'] ?? '卡密购买'); ?>
                </a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarNav">
                    <ul class="navbar-nav ms-auto">
                        <li class="nav-item">
                            <a class="nav-link" href="../index.php">返回首页</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="orders.php">我的订单</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
        
        <!--页面主要内容-->
        <main class="lyear-layout-content">
            <div class="container py-4">
                <div class="success-container mx-auto">
                    <!-- 成功提示 -->
                    <div class="text-center mb-4">
                        <div class="success-icon">
                            <i class="mdi mdi-check-circle"></i>
                        </div>
                        <h2 class="text-success">支付成功！</h2>
                        <p class="text-muted">您的卡密已生成，请妥善保管</p>
                    </div>
                    
                    <!-- 订单信息 -->
                    <div class="card mb-4">
                        <div class="card-header">
                            <h5><i class="mdi mdi-receipt"></i> 订单信息</h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <p><strong>订单号：</strong><?php echo htmlspecialchars($order['order_no']); ?></p>
                                    <p><strong>商品名称：</strong><?php echo htmlspecialchars($order['product_name']); ?></p>
                                </div>
                                <div class="col-md-6">
                                    <p><strong>购买数量：</strong><?php echo $order['quantity']; ?> 张</p>
                                    <p><strong>支付金额：</strong><span class="text-danger fw-bold">￥<?php echo number_format($order['total_amount'], 2); ?></span></p>
                                </div>
                            </div>
                            <p><strong>支付时间：</strong><?php echo date('Y-m-d H:i:s', strtotime($order['paid_at'])); ?></p>
                        </div>
                    </div>
                    
                    <!-- 卡密信息 -->
                    <div class="card">
                        <div class="card-header">
                            <h5><i class="mdi mdi-credit-card"></i> 您的卡密</h5>
                        </div>
                        <div class="card-body">
                            <?php if (!empty($order['kami_list'])): ?>
                                <div class="kami-card">
                                    <div class="d-flex justify-content-between align-items-center mb-3">
                                        <h6 class="mb-0">卡密列表</h6>
                                        <button type="button" class="btn btn-sm btn-outline-primary" onclick="copyAndGoHome()">
                                            <i class="mdi mdi-rocket-launch"></i> 立即使用
                                        </button>
                                    </div>
                                    <?php 
                                    $kami_list = explode("\n", $order['kami_list']);
                                    foreach ($kami_list as $kami): 
                                        if (trim($kami)):
                                    ?>
                                        <div class="kami-item d-flex justify-content-between align-items-center">
                                            <span><?php echo htmlspecialchars(trim($kami)); ?></span>
                                            <button type="button" class="btn btn-sm btn-link copy-btn" onclick="copyKami('<?php echo htmlspecialchars(trim($kami)); ?>', this)">
                                                <i class="mdi mdi-content-copy"></i>
                                            </button>
                                        </div>
                                    <?php 
                                        endif;
                                    endforeach; 
                                    ?>
                                </div>
                                
                                <div class="alert alert-info">
                                    <h6><i class="mdi mdi-information-outline"></i> 使用说明</h6>
                                    <ul class="mb-0">
                                        <li>请妥善保管您的卡密，不要泄露给他人</li>
                                        <li>如有问题请联系客服</li>
                                    </ul>
                                </div>
                            <?php else: ?>
                                <div class="alert alert-warning">
                                    <i class="mdi mdi-alert-circle"></i> 卡密生成中，请稍后刷新页面查看
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <!-- 操作按钮已删除 -->
                </div>
            </div>
        </main>
    </div>
</div>

<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/popper.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
<script src="../assets/js/perfect-scrollbar.min.js"></script>
<script src="../assets/js/main.min.js"></script>
<script>
// 复制单个卡密
function copyKami(kami, btn) {
    navigator.clipboard.writeText(kami).then(function() {
        // 显示复制成功提示
        if (btn) {
            const originalHtml = btn.innerHTML;
            btn.innerHTML = '<i class="mdi mdi-check"></i>';
            btn.style.color = '#28a745';
            
            setTimeout(function() {
                btn.innerHTML = originalHtml;
                btn.style.color = '#007bff';
            }, 1000);
        }
    }).catch(function(err) {
        alert('复制失败，请手动复制');
    });
}

// 复制全部卡密并跳转首页
function copyAndGoHome() {
    const kamiElements = document.querySelectorAll('.kami-item span');
    const kamiList = Array.from(kamiElements).map(el => el.textContent.trim()).join('\n');
    navigator.clipboard.writeText(kamiList).then(function() {
        window.location.href = '../index.php';
    }).catch(function(err) {
        window.location.href = '../index.php';
    });
}

// 如果卡密为空，自动刷新页面
<?php if (empty($order['kami_list'])): ?>
setTimeout(function() {
    location.reload();
}, 5000); // 5秒后刷新
<?php endif; ?>
</script>
</body>
</html> 